package org.dftproject.genesis.ui.widgets;

import org.dftproject.genesis.data.genealogy.GenealogyConstants;
import org.dftproject.genesis.data.genealogy.GenealogyUtils;
import org.dftproject.genesis.data.genealogy.IName;
import org.dftproject.genesis.data.genealogy.IPerson;
import org.dftproject.genesis.data.genealogy.Sex;
import org.dftproject.genesis.ui.SharedImages;
import org.dftproject.genesis.ui.pages.PageManager;
import org.dftproject.genesis.ui.pages.summary.SummaryPage;
import org.dftproject.genesis.ui.pages.summary.SummaryPageInput;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.forms.events.HyperlinkAdapter;
import org.eclipse.ui.forms.events.HyperlinkEvent;
import org.eclipse.ui.forms.widgets.ImageHyperlink;

public class PersonHyperlink extends Composite {

	private final ImageHyperlink link;
	private final PersonToolTip tooltip;
	
	private IPerson person;
	
	public PersonHyperlink(Composite parent, int style) {
		super(parent, style);
		
		RowLayout layout = new RowLayout(SWT.HORIZONTAL);
		layout.marginTop = layout.marginRight = layout.marginBottom = layout.marginLeft = layout.marginWidth = layout.marginHeight = 0;
		layout.spacing = 4;
		this.setLayout(layout);
		
		link = new ImageHyperlink(this, SWT.NONE);
		link.setText("(Unknown)");
		link.setImage(SharedImages.getDefault().getImage(SharedImages.IMG_NULL));
		link.addHyperlinkListener(new HyperlinkAdapter() {

			@Override
			public void linkActivated(HyperlinkEvent event) {
				if (person == null)
					return;
				
				IWorkbench workbench = PlatformUI.getWorkbench();
				IWorkbenchWindow window = workbench.getActiveWorkbenchWindow();
				IWorkbenchPage workbenchPage = window.getActivePage();
				PageManager.getDefault().openPage(workbenchPage, new SummaryPageInput(person), SummaryPage.ID, true, (event.getStateMask() & SWT.CTRL) != 0);
			}
			
		});
		
		tooltip = new PersonToolTip(link, PersonToolTip.NO_RECREATE, false);
		tooltip.setPopupDelay(200);
	}

	public void setPerson(final IPerson person) {
		this.person = person;
		tooltip.setPerson(person);
		
		final Display display = getDisplay();
		
		Job job = new Job("Populate hyperlink") {
			
			private String fullName;
			private Sex sex;

			@Override
			protected IStatus run(IProgressMonitor monitor) {
				IName name = (IName) GenealogyUtils.getValue(person, GenealogyConstants.name, IName.class);
				if (name != null)
					fullName = GenealogyUtils.stringFromName(name);

				sex = (Sex) GenealogyUtils.getValue(person, GenealogyConstants.sex, Sex.class);
				
				display.asyncExec(new Runnable() {

					public void run() {
						if (fullName != null && !"".equals(fullName))
							link.setText(fullName);
						
						if (sex == Sex.Male)
							link.setImage(SharedImages.getDefault().getImage(SharedImages.IMG_MALE));
						else if (sex == Sex.Female)
							link.setImage(SharedImages.getDefault().getImage(SharedImages.IMG_FEMALE));
							
						getParent().layout();
					}
					
				});
				
				return Status.OK_STATUS;
			}
			
		};
		job.schedule();
	}
	
	public ImageHyperlink getImageHyperlink() {
		return link;
	}

}
